<template>
  <a-modal
    :title="title"
    :width="460"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    okText="保存"
    wrapClassName="ant-modal-cust-warp">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="*互斥项名称">
          <a-input placeholder="请输入互斥项名称" v-model="huchi_name"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="系统编号"
          hasFeedback >
          <a-input placeholder="请输入系统编号" v-model="huchi_code"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="*定制项"
          hasFeedback >
          <a-select @change="show_canshulist(select_topcategory1)" v-model="select_topcategory1" style="color: #827d7d" placeholder="请选择定制项">
            <a-select-option :value="item.id" v-for="item in categoryList" >{{ item.parameter }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="*选择参数"
          hasFeedback >
          <a-select v-model="select_huchi1" style="color: #827d7d" placeholder="请选择参数">
            <a-select-option :value="item.id" v-for="item in dingzhi_canshuList" >{{ item.categoryName }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="*互斥定制项"
          hasFeedback >
          <a-select @change="show_canshulist2(select_topcategory2)" v-model="select_topcategory2" style="color: #827d7d" placeholder="请选择互斥定制项">
            <a-select-option :value="item.id" v-for="item in categoryList" >{{ item.parameter }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="*互斥参数"
          hasFeedback >

          <a-select
            mode="multiple"
            style="width: 100%"
            placeholder="请选择互斥参数"
            v-model="select_huchi2"
            optionFilterProp = "children"
            :getPopupContainer = "(target) => target.parentNode"
            :dropdownStyle="{maxHeight:'200px',overflow:'auto'}" onchange="change_canshu()">
            <a-select-option v-for="item in dingzhi_canshuList2" :value="item.id">{{item.categoryName}}</a-select-option>
          </a-select>


<!--          <a-select v-model="select_huchi2" style="color: #827d7d" placeholder="请选择互斥参数">-->
<!--            <a-select-option :value="item.id" v-for="item in dingzhi_canshuList2" >{{ item.categoryName }}</a-select-option>-->
<!--          </a-select>-->
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="*状态"
          hasFeedback >
          <a-select v-model="select_status" style="color: #827d7d" placeholder="请选择互斥状态">
            <a-select-option :value="item.id" v-for="item in statuslist" >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注"
          hasFeedback >
          <a-textarea placeholder="请输入备注信息" v-model="remark" rows="3"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, postAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "JeecgDemoModal",
    components: {
    },
    data () {
      return {
        huchi_name:'',
        huchi_code:'',
        select_topcategory1:'请选择定制项',
        select_huchi1:'请选择参数',
        select_topcategory2:'请选择互斥定制项',
        select_huchi2:[],
        select_status:'请选择互斥状态',
        remark:'',
        canshu2:'',
        dataDetail:[],//数据对象
        title:"操作",
        visible: false,
        model: {},
        validatorRules:{
          roleName:{
            rules: [
              { required: true, message: '请输入角色名称!' },
              { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
            ]},
          roleCode:{
            rules: [
              { required: true, message: '请输入角色名称!'},
              { min: 0, max: 64, message: '长度不超过 64 个字符', trigger: 'blur' },
              { validator: this.validateRoleCode}
            ]},
          description:{
            rules: [
              { min: 0, max: 126, message: '长度不超过 126 个字符', trigger: 'blur' }
            ]}
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        statuslist: [
          {
            id: '1',
            name: '允许'
          },
          {
            id: '2',
            name: '禁止'
          }
        ],
        categoryList: [],//定制列表
        dingzhi_canshuList: [],//定制下的参数列表
        dingzhi_canshuList2: [],//定制下的参数列表2
        confirmLoading: false,
        form: this.$form.createForm(this),
        url: {
          query_detail:"/admin/parameter/query_huchiByid",
          add: "/admin/parameter/add_huchi",
          edit: "/admin/parameter/update_huchi",
          query_dingzhiList:"/admin/parameter/query_dingzhiBypingleiId",
          canshu_list: "/admin/parameter/test_dignzhipartmer",
          allcanshu_dingzhi:"/admin/parameter/test_all_dignzhipartmer"
        },
      }
    },
    computed: {

    },
    created () {

    },
    methods: {
      edit (record) {
        this.showdingzhi_list();//查询定制项
        this.huchi_name='';
        this.huchi_code='';
        this.select_topcategory1='请选择定制项';
        this.select_huchi1='请选择参数';
        this.select_topcategory2='请选择互斥定制项';
        this.select_huchi2=[];
        this.select_status='请选择互斥状态';
        this.remark='';
        this.canshu2='';
        this.model = Object.assign({}, record);
        this.visible = true;//开启弹框
        if(record=="0"){
          this.title="新增互斥项"
        }else {
          this.title="编辑互斥项"
          this.show_datainfodetail(this.model.id);
        }
      },
      showdingzhi_list() {//查询定制项列表
        let params = {category_id:this.$route.query.category_id};
        getAction(this.url.query_dingzhiList,params).then((res)=>{
          if(res.success){
            this.categoryList = res.result;
          }else {
            this.categoryList = [];
          }
        });
      },
      show_canshulist(dingzhiid) {
        let params = {module_id:dingzhiid};
        getAction(this.url.allcanshu_dingzhi,params).then((res)=>{
          console.info(res);
          if(res.success){
            this.select_huchi1="请选择参数"
            this.dingzhi_canshuList = res.result;
          }else {
            this.$message.error("查询失败2");
          }
        });
      },
      show_canshulist2(dingzhiid) {
        let params = {module_id:dingzhiid};
        getAction(this.url.allcanshu_dingzhi,params).then((res)=>{
          console.info(res);
          if(res.success){
            this.select_huchi2==[]
            this.dingzhi_canshuList2 = res.result;
          }else {
            this.$message.error("查询失败2");
          }
        });
      },
      show_datainfodetail(id) {//查询详情
        let params = {huchi_id:id};
        getAction(this.url.query_detail,params).then((res)=>{
          if(res.success){
            this.huchi_name=res.result.mutexName;
            this.huchi_code=res.result.code;
            this.select_topcategory1=res.result.dingzhiName1;
            this.select_huchi1=res.result.categoryName;
            this.select_topcategory2=res.result.dingzhiName2;
            this.select_huchi2=res.result.categoryOtherName;
            this.canshu2 = res.result.categoryOtherName;
            if(res.result.status=='1'){
              this.select_status='允许'
            }else if(res.result.status=='2'){
              this.select_status='禁止'
            }
            this.remark=res.result.mutexReason;
          }else {
            this.$message.error("查询失败2");
          }
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if(this.huchi_name==null || this.huchi_name==''){
            this.$message.warning("请输入互斥项名称");
            return;
          }else if(this.select_topcategory1=='' || this.select_topcategory1=='请选择定制项'){
            this.$message.warning("请选择定制项");
            return;
          }else if(this.select_huchi1=='' || this.select_huchi1=='请选择参数'){
            this.$message.warning("请选择参数");
            return;
          }else if(this.select_topcategory2=='' || this.select_topcategory2=='请选择互斥定制项'){
            this.$message.warning("请选择互斥定制项");
            return;
          }else if(this.select_huchi2=='' || this.select_huchi2==null || this.select_huchi2==[]){
            this.$message.warning("请选择互斥参数");
            return;
          }else if(this.select_status=='' || this.select_status=='请选择互斥状态'){
            this.$message.warning("请选择互斥状态");
            return;
          }
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            let params = '';
            if(this.model.id!=null && this.model.id!=''){
              httpurl+=this.url.edit;
              method = 'put';
              if(this.select_status=='允许'){
                this.select_status='1';
              }else if(this.select_status=='禁止'){
                this.select_status='2';
              }
              if(this.canshu2==null || this.canshu2==''){
                this.select_huchi2=this.select_huchi2.join(",");
              }
              // alert(this.model.id+"==="+this.select_huchi2)
              params = {mutexName:this.huchi_name,mutexReason:this.remark,code:this.huchi_code,
                dingzhi1:this.select_topcategory1,dingzhi2:this.select_topcategory2,categoryId:this.select_huchi1,
                categoryOther:this.select_huchi2,status:this.select_status,id:this.model.id}
            }else{
              httpurl+=this.url.add;
              method = 'post';
              this.select_huchi2=this.select_huchi2.join(",");
              params = {mutexName:this.huchi_name,mutexReason:this.remark,code:this.huchi_code,
                dingzhi1:this.select_topcategory1,dingzhi2:this.select_topcategory2,categoryId:this.select_huchi1,
                categoryOther:this.select_huchi2,topId:this.$route.query.category_id,status:this.select_status}
            }
            httpAction(httpurl,params,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.$emit('ok');
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>